package com.weakteam.orderonline.adminconsole.gwt.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Place;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Requester;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Responser;

/**
 * Created by ZabbarovMZ on 22.04.2014.
 */
public class PlacesWidget extends Composite implements Responser {

    interface TablesWidgetUiBinder extends UiBinder<Widget, PlacesWidget> { }
    private static TablesWidgetUiBinder uiBinder = GWT.create(TablesWidgetUiBinder.class);

    @UiField  protected PlacesControl placesControl;
    @UiField  protected AbsolutePanel dragSpace;
    protected PlacesContainer placesContainer;
    protected PlacesBox selectedBox;

    @UiFactory
    PlacesControl makeTablesControl() { // method name is insignificant
        return new PlacesControl();
    }


    public PlacesWidget() {
        initWidget(uiBinder.createAndBindUi(this));
        this.placesContainer = new PlacesContainer(dragSpace);
        // load data
        this.placesControl.setItemSelectedMode(false);
        Requester request = new Requester();
        request.addResponser(this);
        request.getPlacesAll();
        // added handlers
        this.placesControl.addButton.addClickHandler (new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                addNewItem();
            }
        });
        this.placesControl.editButton.addClickHandler (new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                editSelectedItem(selectedBox);
            }
        });
        this.placesControl.removeButton.addClickHandler (new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                deleteSelectedItem(selectedBox);
                placesControl.setItemSelectedMode(false);
            }
        });
        this.placesContainer.addSelectionHandler (new SelectionHandler<PlacesBox>() {
            @Override
            public void onSelection(SelectionEvent<PlacesBox> event) {
                selectedBox = event.getSelectedItem();
                placesControl.setItemSelectedMode(true);
            }
        });
        this.placesContainer.addOpenHandler(new OpenHandler<Place>() {
            @Override
            public void onOpen(OpenEvent<Place> event) {
                editSelectedItem(selectedBox);
            }
        });
    }

    @Override
    public void handleGetResponse(JSONArray data) {
        selectedBox = null;
        placesControl.setItemSelectedMode(false);
        placesContainer.setData(data);
    }
    @Override
    public void handlePostResponse(int responseCode) {
        if (200 == responseCode) {
            Requester request = new Requester();
            request.addResponser(this);
            request.getPlacesAll();
        }
    }

    private void addNewItem() {
        Place newCategory = new Place();
        PlacesPopup popup = new PlacesPopup(newCategory);
        popup.addValueChangeHandler(new ValueChangeHandler<Place>() {
            @Override
            public void onValueChange(ValueChangeEvent<Place> event) {
                Place newValue = event.getValue();
                // send post request to server
                Requester request = new Requester();
                request.addResponser(PlacesWidget.this);
                request.setPlaceOne(newValue.getJSON());
            }
        });
    }

    private void editSelectedItem(PlacesBox selectedBox) {
        if (selectedBox != null) {
            PlacesPopup popup = new PlacesPopup(selectedBox.getPlace());
            popup.addValueChangeHandler(new ValueChangeHandler<Place>() {
                @Override
                public void onValueChange(ValueChangeEvent<Place> event) {
                    Place newValue = event.getValue();
                    // send post request to server
                    Requester request = new Requester();
                    request.addResponser(PlacesWidget.this);
                    request.setPlaceOne(newValue.getJSON());
                }
            });
        }
    }

    private void deleteSelectedItem(PlacesBox selectedBox) {
        if (selectedBox != null) {
            // send post request to server
            Requester request = new Requester();
            request.addResponser(PlacesWidget.this);
            request.deletePlaceById(selectedBox.getPlaceId());
        }
    }
}
